package com.servlet;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import com.info.UserInfo;

/**
 *使用HttpSessionBindingListener类型的接口, 当该类对象从session中解绑时, 调用响应函数
 *用户登录时, 创建相应对象,绑定到session,开始监听
 */
@WebListener
public class UserListener implements HttpSessionBindingListener {
    private String username;
    private UserInfo userlist = UserInfo.getInstance();
    /**
     * Default constructor. 
     */
    public UserListener() {
        // TODO Auto-generated constructor stub
    	this.username = "";
    }

    public void setUser(String username) {
    	this.username = username;
    }
    
    public String getUser() {
    	return this.username;
    }
	/**
     * @see HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
     * 绑定到session时触发
     */
    public void valueBound(HttpSessionBindingEvent event)  { 
         // TODO Auto-generated method stub
    	System.out.println("用户["+this.username+"] 已登录");
    }

	/**
     * @see HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
     * 从session解除绑定时触发
     */
    public void valueUnbound(HttpSessionBindingEvent event)  { 
         // TODO Auto-generated method stub
    	System.out.println("用户"+this.username+"] 已退出登录");
    	userlist.removeUser(this.username);
    }
	
}
